package com.feonix.complat.controller.role;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.feonix.common.annotation.Permission;
import com.feonix.common.basedao.LikeType;
import com.feonix.common.service.GridViewService;
import com.feonix.common.util.NumberUtil;
import com.feonix.common.util.StringUtil;
import com.feonix.common.util.mvc.Script;
import com.feonix.common.view.grid.Button;
import com.feonix.common.view.grid.GridRow;
import com.feonix.common.view.grid.GridView;
import com.feonix.common.view.grid.GridViewDelegate;
import com.feonix.common.view.grid.GridViewSql;
import com.feonix.common.view.grid.Head;
import com.feonix.complat.constant.Tables;

/**
 * 
 * @CopyRight (c)1993-2017
 * @Project m-complat-controller
 * @Comments 角色列表页
 * @NameSpace com.feonix.complat.controller.role
 * @Author Feonixzw
 * @CreateDate 2017年12月20日
 */
@Controller
@Permission(module = "right")
@RequestMapping("manager/main/right")
public class ListRightController implements GridViewDelegate {

	@Autowired
	private GridViewService gridViewService;

	@Permission(function = "right_list")
	@RequestMapping("right_list")
	public GridView list(GridView gridView) {
		gridView.setDelegate(this);
		gridView.setViewName("complat/right/right_list");
		createButton(gridView);
		createHead(gridView);
		createBody(gridView);
		gridView.setShowAdvSearch(false);
		gridView.setPosition("菜单权限管理");
		gridView.setSearchPlaceholder("请输入菜单权限名称");
		return gridView;
	}

	/**
	 * 创建按钮
	 * 
	 * @param gridView
	 */
	private void createButton(GridView gridView) {
		gridView.addButton(Button.getAdd());
		gridView.addButton(Button.getRemove());
	}

	/**
	 * 创建表头
	 * 
	 * @param gridView
	 */
	private void createHead(GridView gridView) {
		gridView.addHead(Head.getInstance().setCheckbox(true).setField("iid"));
		gridView.addHead(Head.getInstance().setField("moduleid").setTitle("模块标识").setAlign("center").setWidth(200)
				.setResizable(true));
		gridView.addHead(Head.getInstance().setField("modulename").setTitle("模块名称").setAlign("center").setWidth(200)
				.setResizable(true));
		gridView.addHead(Head.getInstance().setField("functionid").setTitle("方法标识").setAlign("center").setWidth(100));
		gridView.addHead(Head.getInstance().setField("functionname").setTitle("方法名称").setAlign("center").setWidth(100));
	}

	/**
	 * 创建列表
	 * 
	 * @param gridView
	 */
	private void createBody(GridView gridView) {
		GridViewSql gridViewSql = GridViewSql.getInstance(gridView);
		gridViewSql.addSelectField("iid").addSelectField("codeid").addSelectField("parentcodeid").addSelectField("moduleid")
					.addSelectField("modulename").addSelectField("functionid").addSelectField("functionname")
				.setTable(Tables.RIGHT);
		if (StringUtil.isNotEmpty(gridView.getSearchText())) {
			gridViewSql.setWhere("modulename LIKE :modulename or functionname LIKE :functionname ");
			gridViewSql.addParam("modulename", gridView.getSearchText(), LikeType.LR);
			gridViewSql.addParam("functionname", gridView.getSearchText(), LikeType.LR);
		}
		gridViewSql.addOrderBy("iid", "ASC");
		gridViewService.find(gridViewSql);
	}

	@Override
	public void createRow(GridRow gridRow, Map<String, Object> rowData, Integer index) {
		String iid = StringUtil.getString(rowData.get("iid"));
		String moduleid = StringUtil.getString(rowData.get("moduleid"));
		String modulename = StringUtil.getString(rowData.get("modulename"));
		String functionid = StringUtil.getString(rowData.get("functionid"));
		String functionname = StringUtil.getString(rowData.get("functionname"));
		gridRow.addCell("iid", iid);
		gridRow.addCell("moduleid", moduleid, Script.createScript("edit", iid));
		gridRow.addCell("modulename", modulename, Script.createScript("edit", iid));
		gridRow.addCell("functionid", functionid, Script.createScript("edit", iid));
		gridRow.addCell("functionname", functionname, Script.createScript("edit", iid));
	}
}
